package com.adcc.loadsheet.movingdata.service;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.movingdata.congifuration.AppConfig;
import com.adcc.loadsheet.movingdata.entity.*;
import com.adcc.loadsheet.movingdata.mapper.mysql.MysqlMapper;
import com.adcc.loadsheet.movingdata.mapper.oracle.OracleMapper;
import com.adcc.loadsheet.movingdata.util.FileUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * Created by AN on 2020-05-04.
 */
@Service
public class LoadsheetServiceImpl implements LoadsheetService {

    @Autowired
    private MysqlMapper mysqlMapper;

    @Autowired
    private OracleMapper oracleMapper;

    @Autowired
    private AppConfig appConfig;

    private String format="yyyy-MM-dd";

    @Override
    public void appFirstRun() {
        DateTime now=DateTime.now();
        DateTime last=FileUtil.getLastDay();
        if(last==null) return;
        if(last.isBefore(now.minusDays(appConfig.getKeepDays()))) return;
        List<ScUpDataEntity> nowitem=oracleMapper.findLoadsheet(last.toString(format),now.toString(format));
        if(nowitem.size()<=0) return;
        try{
            mysqlMapper.deleteLoadsheet(last.toString(format),now.toString(format));
            mysqlMapper.deleteHisLoadsheet(now.minusDays(appConfig.getKeepDays()).toString(format));
            for(ScUpDataEntity one :nowitem){
                mysqlMapper.insertLoadsheet(one);
            }
            FileUtil.saveLastDay(now.toString(format));
            mysqlMapper.deleteFailDate(last.toString(format),now.toString(format));
        }catch (Exception ex){
            insertFaildate(last.toString(format),now.toString(format));
            throw ex;
        }
    }

    @Override
    public int moveLoadsheetDataBetween(String start, String end) {
        List<ScUpDataEntity> nowitem=oracleMapper.findLoadsheet(start,end);
        if(nowitem.size()<=0) return 0;
        mysqlMapper.deleteLoadsheet(start,end);
        for(ScUpDataEntity one :nowitem){
            mysqlMapper.insertLoadsheet(one);
        }
        mysqlMapper.deleteFailDate(start,end);
        return nowitem.size();
    }

    @Override
    public void moveLoadsheetData() {
        DateTime now=DateTime.now();
        try{
            List<ScUpDataEntity> nowitem=oracleMapper.findLoadsheet(now.minusDays(1).toString(format),now.toString(format));
            if(nowitem.size()<=0) return;
            mysqlMapper.deleteLoadsheet(now.minusDays(1).toString(format),now.toString(format));
            mysqlMapper.deleteHisLoadsheet(now.minusDays(appConfig.getKeepDays()).toString(format));
            for(ScUpDataEntity one :nowitem){
                mysqlMapper.insertLoadsheet(one);
            }
            FileUtil.saveLastDay(now.toString(format));
            mysqlMapper.deleteFailDate(now.minusDays(1).toString(format),now.toString(format));
        }catch (Exception ex){
            mysqlMapper.insertFaildate(UUID.randomUUID().toString(),now.minusDays(1).toString(format));
            throw ex;
        }

    }



    @Override
    public void moveStaff() {
        List<PbStaffEntity> nowstaff=oracleMapper.findAllStaff();
        List<PbStaffEntity> oldstaff=mysqlMapper.findAllStaff();
        PbStaffEntity samestaff=null;
        for(PbStaffEntity one : nowstaff){
            samestaff=null;
            for(PbStaffEntity oldone:oldstaff){
                if(one.getPB_LOGIN_ID().equals(oldone.getPB_LOGIN_ID())){
                    if(CompareStaff(one,oldone)){
                        mysqlMapper.updateStaff(one);
                    }
                    samestaff=oldone;
                    break;
                }
            }
            if(samestaff!=null){
                oldstaff.remove(samestaff);
            }else{
                mysqlMapper.insertStaff(one);
            }
        }

        for(PbStaffEntity delone:oldstaff){
            mysqlMapper.deleteStaff(delone);
        }
    }

    private boolean CompareStaff(PbStaffEntity one, PbStaffEntity oldone) {
        if(!CompareString(one.getPB_PASSWORD(),oldone.getPB_PASSWORD())){
            return true;
        }

        if(!CompareString(one.getPB_NAME(),oldone.getPB_NAME())){
            return true;
        }

        if(!CompareString(one.getPB_DEPARTMENT_ID(),oldone.getPB_DEPARTMENT_ID())){
            return true;
        }

        if(!CompareString(one.getPB_IS_INNER_USER(),oldone.getPB_IS_INNER_USER())){
            return true;
        }

        if(!CompareString(one.getPB_SEX(),oldone.getPB_SEX())){
            return true;
        }
        return false;
    }

    private boolean CompareString(Object one,Object other){
        if(one==other){
            return true;
        }
        if(one!=null&&one.equals(other)){
            return true;
        }
        return false;
    }

    @Override
    public void moveDepartment() {
        List<PbDepartmentEntity> nowitem=oracleMapper.findAllDepart();
        List<PbDepartmentEntity> olditem=mysqlMapper.findAllDepart();
        PbDepartmentEntity sameitem=null;
        for(PbDepartmentEntity one : nowitem){
            sameitem=null;
            for(PbDepartmentEntity oldone:olditem){
                if(one.getPB_ID().equals(oldone.getPB_ID())){
                    if(CompareDepart(one,oldone)){
                        mysqlMapper.updateDepart(one);
                    }
                    sameitem=oldone;
                    break;
                }
            }
            if(sameitem!=null){
                olditem.remove(sameitem);
            }else{
                mysqlMapper.insertDepart(one);
            }
        }

        for(PbDepartmentEntity delone:olditem){
            mysqlMapper.deleteDepart(delone);
        }
    }

    private boolean CompareDepart(PbDepartmentEntity one, PbDepartmentEntity oldone) {
        if(!CompareString(one.getPB_NAME(),oldone.getPB_NAME())){
            return true;
        }
        if(!CompareString(one.getPB_PARENT_ID(),oldone.getPB_PARENT_ID())){
            return true;
        }
        return false;
    }

    @Override
    public void moveAirlines() {
        List<ScAirlinesEntity> nowitem=oracleMapper.findAllAirline();
        List<ScAirlinesEntity> olditem=mysqlMapper.findAllAirline();
        ScAirlinesEntity sameitem=null;
        for(ScAirlinesEntity one : nowitem){
            sameitem=null;
            for(ScAirlinesEntity oldone:olditem){
                if(one.getId().equals(oldone.getId())){
                    if(CompareAirline(one,oldone)){
                        mysqlMapper.updateAirline(one);
                    }
                    sameitem=oldone;
                    break;
                }
            }
            if(sameitem!=null){
                olditem.remove(sameitem);
            }else{
                mysqlMapper.insertAirline(one);
            }
        }

        for(ScAirlinesEntity delone:olditem){
            mysqlMapper.deleteAirline(delone);
        }
    }

    private boolean CompareAirline(ScAirlinesEntity one, ScAirlinesEntity oldone) {
        if(!CompareString(one.getAdccCode(),oldone.getAdccCode())){
            return true;
        }
        if(!CompareString(one.getChsName(),oldone.getChsName())){
            return true;
        }
        if(!CompareString(one.getChsShortname(),oldone.getChsShortname())){
            return true;
        }
        if(!CompareString(one.getCountry(),oldone.getCountry())){
            return true;
        }
        if(!CompareString(one.getEngName(),oldone.getEngName())){
            return true;
        }
        if(!CompareString(one.getEngShortname(),oldone.getEngShortname())){
            return true;
        }
        if(!CompareString(one.getIsInternal(),oldone.getIsInternal())){
            return true;
        }
        return false;
    }

    @Override
    public void moveAirports() {
        List<ScAirportEntity> nowitem=oracleMapper.findAllAirport();
        List<ScAirportEntity> olditem=mysqlMapper.findAllAirport();
        ScAirportEntity sameitem=null;
        for(ScAirportEntity one : nowitem){
            sameitem=null;
            for(ScAirportEntity oldone:olditem){
                if(one.getId().equals(oldone.getId())){
                    if(CompareAirport(one,oldone)){
                        mysqlMapper.updateAirport(one);
                    }
                    sameitem=oldone;
                    break;
                }
            }
            if(sameitem!=null){
                olditem.remove(sameitem);
            }else{
                mysqlMapper.insertAirport(one);
            }
        }

        for(ScAirportEntity delone:olditem){
            mysqlMapper.deleteAirport(delone);
        }
    }

    private boolean CompareAirport(ScAirportEntity one, ScAirportEntity oldone) {
        if(!CompareString(one.getAddContent(),oldone.getAddContent())){
            return true;
        }
        if(!CompareString(one.getAirportName(),oldone.getAirportName())){
            return true;
        }
        if(!CompareString(one.getAirportShortname(),oldone.getAirportShortname())){
            return true;
        }
        if(!CompareString(one.getIsAddContent(),oldone.getIsAddContent())){
            return true;
        }
        if(!CompareString(one.getRemark(),oldone.getRemark())){
            return true;
        }
        return false;
    }

    @Override
    public void insertFaildate(String startTime, String endTime) {
        try{
            mysqlMapper.insertFaildate(UUID.randomUUID().toString(),startTime);
            DateTime start=DateTime.parse(startTime).plusDays(1);
            DateTime end=DateTime.parse(endTime);
            while (start.isBefore(end)){
                mysqlMapper.insertFaildate(UUID.randomUUID().toString(),start.toString(format));
                start=start.plusDays(1);
            }
        }catch (Exception ex){
            Log.error(LoadsheetService.class,"insertFaildate() failed",ex);
        }
    }

    @Override
    public void updatebasetime() {
        try{
            mysqlMapper.updatebasetime();
        }catch (Exception ex){
            Log.error(LoadsheetService.class,"updatebasetime() failed",ex);
        }
    }
}
